package com.itheima.test.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisUtil {
    private static JedisPool jedisPool;
    static {
        Properties properties = new Properties();
        InputStream in = JedisUtil.class.getClassLoader().getResourceAsStream("redis.properties");

        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String host = properties.getProperty("redis.host");
        int  port = Integer.parseInt(properties.getProperty("redis.port"));
        int timeout =Integer.parseInt(properties.getProperty("redis.timeout"));
        String password = properties.getProperty("redis.auth");

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//         public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password)
        jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
    }
    //2 创建JedisPool连接池对象
        /*  GenericObjectPoolConfig poolConfig:连接池的配置对象，封装“最大连接数”、“最小连接数”...
            String host:主机名
            int port：端口号
            int timeout：连接超时时间
            String password：连接的密码
            public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password) {
                this(poolConfig, host, port, timeout, password, 0, (String)null);
            }
         */

        public static Jedis getJedis(){
            return  jedisPool.getResource();
        }

        public static void close(Jedis jedis){
            if (jedis != null){
                jedis.close();
            }
        }
}
